package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.PurchaseBo;
import com.ruoyi.supplier.domain.vo.PurchaseVo;
import com.ruoyi.supplier.domain.Purchase;
import com.ruoyi.supplier.mapper.PurchaseMapper;
import com.ruoyi.supplier.service.IPurchaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采购单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class PurchaseServiceImpl implements IPurchaseService {

    private final PurchaseMapper baseMapper;

    /**
     * 查询采购单
     */
    @Override
    public PurchaseVo queryById(Integer purchaseId){
        return baseMapper.selectVoById(purchaseId);
    }

    /**
     * 查询采购单列表
     */
    @Override
    public TableDataInfo<PurchaseVo> queryPageList(PurchaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Purchase> lqw = buildQueryWrapper(bo);
        Page<PurchaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采购单列表
     */
    @Override
    public List<PurchaseVo> queryList(PurchaseBo bo) {
        LambdaQueryWrapper<Purchase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Purchase> buildQueryWrapper(PurchaseBo bo) {
        LambdaQueryWrapper<Purchase> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMerchantId() != null, Purchase::getMerchantId, bo.getMerchantId());
        lqw.eq(bo.getSupplierId() != null, Purchase::getSupplierId, bo.getSupplierId());
        lqw.eq(bo.getStatus() != null, Purchase::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOther()), Purchase::getOther, bo.getOther());
        lqw.eq(bo.getCheckStatus() != null, Purchase::getCheckStatus, bo.getCheckStatus());
        lqw.eq(bo.getFinishedAt() != null, Purchase::getFinishedAt, bo.getFinishedAt());
        lqw.eq(bo.getReviewStatus() != null, Purchase::getReviewStatus, bo.getReviewStatus());
        lqw.eq(bo.getShopId() != null, Purchase::getShopId, bo.getShopId());
        lqw.eq(bo.getUserId() != null, Purchase::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增采购单
     */
    @Override
    public Boolean insertByBo(PurchaseBo bo) {
        Purchase add = BeanUtil.toBean(bo, Purchase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPurchaseId(add.getPurchaseId());
        }
        return flag;
    }

    /**
     * 修改采购单
     */
    @Override
    public Boolean updateByBo(PurchaseBo bo) {
        Purchase update = BeanUtil.toBean(bo, Purchase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Purchase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采购单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
